<html>
<head><meta charset="utf-8"><title>Debuginfo for generators · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html">Debuginfo for generators</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236454689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236454689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236454689">(Apr 28 2021 at 06:00)</a>:</h4>
<p>Hi, <span class="user-mention" data-user-id="116883">@tmandry</span> .</p>
<p>I'm dealing with <a href="https://github.com/rust-lang/rust/issues/79009">#79009</a> and I've noticed that the reason why GDB 10 failed to print generators is that upvars in generators are not marked artificial but variant parts in generators are marked artificial. GDB 10 thinks generators are enums because generators have variant parts (and that is consistent with rustc). And the way GDB 10 to print enums are to find the first non-artificial field and print it. In generators, the first non-artificial part is an upvar so GDB 10 print an upvar as the body of the generator, which is meaningless.</p>
<p>You can also refer my <a href="https://github.com/rust-lang/rust/issues/79009#issuecomment-826952708">comment</a> in <a href="https://github.com/rust-lang/rust/issues/79009">#79009</a> for a little more details.</p>
<p>In my opinion, we should mark upvars artificial and we should not mark variant parts artificial. So GDB 10 will print something that looks good to me.</p>
<div class="codehilite"><pre><span></span><code>(gdb) p b
$2 = generator_objects::main::generator-0::Suspend0{c: 6, d: 7}
</code></pre></div>
<p>However, I checked the commit history and found that you <a href="https://github.com/rust-lang/rust/commit/eb726c0fcec3f9d12a6a9990c5c041478e1e7187">committed</a> the original code and you <a href="https://github.com/rust-lang/rust/commit/887fbd9d3411ea7def2cc7a508d74bea6b7f19bb">left</a> the following comments in <code>src/test/codegen/async-fn-debug.rs</code>:</p>
<div class="codehilite"><pre><span></span><code>// Verify debuginfo for async fn:
//  - Each variant points to the file and line of its yield point
//  - The generator types and variants are marked artificial
//  - Captured vars from the source are not marked artificial
</code></pre></div>
<p>I wonder if there are any reasons behind the original design.</p>
<p>Hope my question doesn't bother you. And thanks for your explanation in advance.</p>



<a name="236471078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236471078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236471078">(Apr 28 2021 at 08:47)</a>:</h4>
<p>Captured vars are likely not marked as artificial as they do exist in the source code. The generator variants don't exist in the source code and thus are logically artificial. Not marking them as artificial would be fine I guess as the fields of the variants do exist in the source code and thus should likely be shown.</p>



<a name="236473712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236473712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236473712">(Apr 28 2021 at 09:07)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/131828-t-compiler/topic/Debuginfo.20for.20generators/near/236471078">said</a>:</p>
<blockquote>
<p>Captured vars are likely not marked as artificial as they do exist in the source code. The generator variants don't exist in the source code and thus are logically artificial. Not marking them as artificial would be fine I guess as the fields of the variants do exist in the source code and thus should likely be shown.</p>
</blockquote>
<p>I think variants are at least not more artificial than upvars. For example, look at the following code.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">6</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="kr">yield</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">a</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">c</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="n">d</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="kr">yield</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{} {} {}"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>Doesn't the generator <code>b</code> naturally contain <code>c</code> and <code>d</code>? So it is not very artificial.</p>



<a name="236473823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236473823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236473823">(Apr 28 2021 at 09:08)</a>:</h4>
<p>Another problem is that GDB will print the first non-artificial field only. Thus we cannot have two non-artificial fields. Should we simply treat this as a bug of GDB or adjust our debuginfo rules a bit so that GDB can recognize?</p>



<a name="236568756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236568756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236568756">(Apr 28 2021 at 19:59)</a>:</h4>
<p>I've actually been wanting to move the upvars into the variants themselves rather than leave them outside (the same variable would just be in all variants, and we already have variables in multiple variants today)</p>



<a name="236568777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236568777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236568777">(Apr 28 2021 at 19:59)</a>:</h4>
<p>that might improve the integration somewhat</p>



<a name="236568944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236568944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236568944">(Apr 28 2021 at 20:00)</a>:</h4>
<p>by and large I'd prefer to fix the integration in the debugger when possible. but where there are ambiguities we can tweak things.</p>



<a name="236569141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236569141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236569141">(Apr 28 2021 at 20:01)</a>:</h4>
<p>I could maybe be convinced that variants could be marked as not artificial, since they each correspond to a yield point written in the source. It's a bit of a stretch, but practically I don't know of any negative consequences of doing it.</p>



<a name="236602832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236602832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236602832">(Apr 29 2021 at 00:52)</a>:</h4>
<p>I thought we could mark upvars as artificial and mark variants as non-artificial. </p>
<ul>
<li>It is consistent with my intuition (i.e. upvars are more artificial than variants).</li>
<li>It fixes the printing of generators in GDB 10.</li>
<li>At least at present, I think printing upvars makes nearly no sense.  For example, see the following example (corresponding to the code segment that I have given above):</li>
</ul>
<div class="codehilite"><pre><span></span><code>// gdb-check:$1 = generator_objects::main::generator-0 {__0: 0x[...], &lt;&lt;variant&gt;&gt;: {__state: 0, 0: generator_objects::main::generator-0::Unresumed, 1: generator_objects::main::generator-0::Returned, 2: generator_objects::main::generator-0::Panicked, 3: generator_objects::main::generator-0::Suspend0 {[...]}, 4: generator_objects::main::generator-0::Suspend1 {[...]}}}
</code></pre></div>
<p>The upvar <code>&amp;mut a</code> is printed as <code>__0: 0x[...]</code>, which may be hard to understand by an end user (unless he/she checks carefully which local variable the pointer is pointed to).</p>
<ul>
<li>However, printing the variant part can be helpful.</li>
</ul>
<div class="codehilite"><pre><span></span><code>(gdb) p b
$2 = generator_objects::main::generator-0::Suspend0{c: 6, d: 7}
</code></pre></div>



<a name="236602871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236602871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236602871">(Apr 29 2021 at 00:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116883">tmandry</span> <a href="#narrow/stream/131828-t-compiler/topic/Debuginfo.20for.20generators/near/236568756">said</a>:</p>
<blockquote>
<p>I've actually been wanting to move the upvars into the variants themselves rather than leave them outside (the same variable would just be in all variants, and we already have variables in multiple variants today)</p>
</blockquote>
<p>But maybe I shouldn't do what I said above if our final goal is to move the upvars into the variants? Moving upvars into variants does provide better integration. For example, it will work in GDB 8/9 and LLDB without requiring other modification.</p>



<a name="236630291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Debuginfo%20for%20generators/near/236630291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lrh2000 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Debuginfo.20for.20generators.html#236630291">(Apr 29 2021 at 07:23)</a>:</h4>
<p>Maybe I can modify only the debuginfo to move upvars into every variant. Modifying the actual layout of generators is not necessary and it gives us no benefits (I think). Is it OK?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>